package com.base.study.fragment;


import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;

import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.base.CBaseMvpFragment;
import com.base.commonlib.bean.StatEntity;
import com.base.commonlib.utils.RxBus2;
import com.base.commonlib.utils.ToastUtil;
import com.base.study.R;
import com.base.study.databinding.FragmentStudyBinding;

/**
 * Created by zw on 2019/2/12.
 */

public class StudyFragment extends CBaseMvpFragment implements OnClickListener {

    private FragmentManager mFragmentManager;
    private FragmentTransaction mTransaction;
    private MyStudyFragment mMyStudyFragment;
    private OnlineStudyFragment mOnlineStudyFragment;
    private StudyDoubleListFragment studyDoubleListFragment;
    protected FragmentStudyBinding mBinding;

    @Override
    public void init() {
        mBinding = (FragmentStudyBinding) mRootBinding;
    }

    @Override
    public void start() {
        RxBus2.getInstance().post(new StatEntity(2));
        initFragment();
    }

    private void initFragment() {
        mFragmentManager = getChildFragmentManager();
        mTransaction = mFragmentManager.beginTransaction();
        //mMyStudyFragment = new MyStudyFragment();
       // mOnlineStudyFragment = new OnlineStudyFragment();
        studyDoubleListFragment=new StudyDoubleListFragment();
        mTransaction.add(R.id.problem_center_content, studyDoubleListFragment);
        mTransaction.commit();
        mBinding.fgMyStudy.setEnabled(true);
        mBinding.fgOnlineStudy.setEnabled(false);
        mBinding.fgOnlineStudy.setOnClickListener(this);
        mBinding.studyGoSearch.setOnClickListener(this);
        mBinding.fgMyStudy.setOnClickListener(this);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (!hidden){
            RxBus2.getInstance().post(new StatEntity(2));
        }
    }


    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_study;
    }


    @Override
    public void onClick(View v) {
        int i = v.getId();
         if (i == R.id.fg_online_study) {
            mBinding.studyGoSearch.setVisibility(View.VISIBLE);
            if (mBinding.fgOnlineStudy.isEnabled()) {
                mBinding.fgOnlineStudy.setEnabled(false);
                mBinding.fgMyStudy.setEnabled(true);
            }
            mFragmentManager = getActivity().getSupportFragmentManager();
            mTransaction = mFragmentManager.beginTransaction();
//            if (mOnlineStudyFragment == null) {
//                mOnlineStudyFragment = new OnlineStudyFragment();
//                mTransaction.add(R.id.problem_center_content, mOnlineStudyFragment);
//            }
            if (studyDoubleListFragment == null) {
                studyDoubleListFragment = new StudyDoubleListFragment();
                //mTransaction.add(R.id.problem_center_content, studyDoubleListFragment);
            }
            mTransaction.hide(mMyStudyFragment);
            mTransaction.show(studyDoubleListFragment);
            //mTransaction.show(mOnlineStudyFragment);
            mTransaction.commit();

        }else
         if (i == R.id.fg_my_study) {
             mBinding.studyGoSearch.setVisibility(View.INVISIBLE);
             if (mBinding.fgMyStudy.isEnabled()) {
                 mBinding.fgOnlineStudy.setEnabled(true);
                 mBinding.fgMyStudy.setEnabled(false);
             }
             mFragmentManager = getActivity().getSupportFragmentManager();
             mTransaction = mFragmentManager.beginTransaction();
             if (mMyStudyFragment == null) {
                 mMyStudyFragment = new MyStudyFragment();
                 mTransaction.add(R.id.problem_center_content, mMyStudyFragment);
             }
             //  mTransaction.hide(mOnlineStudyFragment);
             mTransaction.hide(studyDoubleListFragment);
             mTransaction.show(mMyStudyFragment);
             mTransaction.commit();


         }
         else if (i == R.id.study_go_search){
            ARouter.getInstance().build("/mystudy/search").navigation();
            //ARouter.getInstance().build("/curriculum/detail").navigation();
//            ARouter.getInstance().build("/curriculum/mynavigation").navigation();
        }
    }
}
